/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.recharge.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.recharge.dao.RechargeDao;
import com.boxin.ims.modules.recharge.entity.Recharge;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 会员充值Service
 * @author Jakemanse
 * @version 2014-03-03
 */
@Component
@Transactional(readOnly = true)
public class RechargeService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(RechargeService.class);
	
	@Autowired
	private RechargeDao rechargeDao;
	
	
	
	public Recharge get(Long id) {
		return rechargeDao.findOne(id);
	}
	
	public	Recharge getRechargeByUserIdAndNumber(Long id,String number){
		return rechargeDao.getRechargeByUserIdAndNumber( id, number);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2014-3-24  下午3:59:57
	 * @function <p>  </p>
	 * @param id
	 * @return
	 */
	public Recharge getRechargeByMemberId(Long id ){
		return rechargeDao.getRechargeByMemberId( id);
	}
	
	public Page<Recharge> find(Page<Recharge> page, Recharge recharge) {
		DetachedCriteria dc = rechargeDao.createDetachedCriteria();
		if (recharge.getUser()!=null && recharge.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", recharge.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(recharge.getName())){
			dc.add(Restrictions.like("name", "%"+recharge.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return rechargeDao.find(page, dc);
	}
	
	
	
	public void generateMemberCardNumber(){
		
	}
	
	/**
	 * @author Jakemanse
	 * @time 2014-3-12  上午9:32:03
	 * @function <p> 保存更新充值 </p>
	 * @param recharge
	 */
	@Transactional(readOnly = false)
	public void save(Recharge recharge) {
		
		rechargeDao.save(recharge);
	}
	
	
	
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		rechargeDao.deleteById(id);
	}
	
	public Recharge getRechargeByNumber(String number){
		return rechargeDao.getRechargeByNumber(number);
	}
	
	
	
}
